# -*- coding = utf-8 -*-
# @Time : 2022/2/23 16:43
# @Author : 龙王赘婿_彪少
# @File : music01.py
# @Software: PyCharm

# 功能：爬取酷狗音乐

search = '喜欢你' #搜索内容
pagesize = '10'  #请求数目
url = 'https://songsearch.kugou.com/song_search_v2?callback=jQuery11240251602301830425_1548735800928&keyword=%s&page=1&pagesize=%s&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1548735800930' % (search,pagesize)
res = requests.get(url) #requests发起get请求
print res.text #输出响应内容

import urllib.request, urllib.error

class MUSIC:
    # 标头
    header = {}
    url = ""
    def __init__(self):
        self.init_value()
        self.getHtml()


    def init_value(self):
        self.header = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
        }
        self.url = "https://searchrecommend.kugou.com/get/complex?callback=jQuery112409993257671333213_1645606919708&word=%E8%AE%B8%E5%B5%A9&_=1645606919712"

    def getHtml(self):
        # 打包一个请求
        request = urllib.request.Request(url=self.url, headers=self.header, method="GET")
        # 接收字符串
        html = ""
        try:
            # 进行请求
            resource = urllib.request.urlopen(request, timeout=10)
            # 解码
            html = resource.read().decode("utf-8")
            # 测试打印
            print(html)
        # 万一出现问题进行处理
        except urllib.error.URLError as e:
            if hasattr(e, "code"):
                print(e.code)
            if hasattr(e, "reason"):
                print(e.reason)
        return html


if __name__ == "__main__":
    print("-"*20 + "begin" + "-"*20)
    obj = MUSIC()
    print("-"*21 + "end" + "-"*21)

